package IO练习.Io流;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class TestCopy {

   public static void main(String[] args) {
      //弄出两个文件，把A.txt中内容复制到B.txt中去
      func();
   }
   public static void func() {
      FileReader fileReader = null;
      FileWriter fileWriter = null;
      try {
         File src = new File("A.txt");//必须要存在
         File dest = new File("B.txt");//一定不可以存在

         fileReader = new FileReader(src);
         fileWriter = new FileWriter(dest);
         char[] chars = new char[5];
         int len;//定义下这个chars数组所能装下的最多字符数
         while ((len = fileReader.read(chars)) != -1) {
            fileWriter.write(chars, 0, len);
         }
      } catch (IOException e) {
         throw new RuntimeException(e);
      } finally {
         if (fileReader != null) {
            try {
               fileReader.close();
            } catch (IOException e) {
               throw new RuntimeException(e);
            }
         }
         if (fileWriter != null) {
            try {
               fileWriter.close();
            } catch (IOException e) {
               throw new RuntimeException(e);
            }
         }
      }
   }
}
